/**
 * PerlaController - part of PerlaGis Architecture
 *
 * Copyright (C) 2010 Ricardo Gonzalez (gomnaz@gmail.com)
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see http://www.gnu.org/licenses/
 */
package org.sql.entities;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Classe che rappresenta lo statement WHERE della query SQL.
 * Contiene un elenco di tutte le condizioni contenute nel WHERE.
 *
 * @author Ricardo Gonzalez
 * @author Eva Gjeci
 */
public class SqlWhere {

    private List<ArrayList> w;

    public SqlWhere() {
        w = new ArrayList<ArrayList>();
    }

    public void add(String key, String value) {
       add(key, value, "=");
    }

    public void add(String key, String value, String operator) {
        ArrayList v = new ArrayList<String>(3);
        v.add(key);
        v.add(value);
        v.add(operator);
        w.add(v);
    }

    /**
     * Costruisce lo statement WHERE, collegando le condizioni tramite AND.
     * Se non contiene restituisce una stringa vuota.
     * 
     *@return lo statement WHERE
     */
    @Override
    public String toString() {
        if (w.isEmpty()) return "";
        
        String r = "WHERE ";
        for (Iterator it = w.iterator(); it.hasNext();) {
            ArrayList a = (ArrayList) it.next();
            r += a.get(0) + " " + a.get(2) + " '" + a.get(1) + "'";
            if (it.hasNext()) {
                r += " AND ";
            }
        }
        return r;
    }
}
